package com.cn.serverline.lineicp.productionLine.handler;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cn.serverline.lineicp.productionLine.entity.ProcessMaterials;
import com.cn.serverline.lineicp.workshopPlan.entity.AuxiliaryMaterials;
import com.cn.serverline.lineicp.workshopPlan.mapper.AuxiliaryMaterialsMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * AuxiliaryMaterialsHandler 类，实现了处理辅助物料的具体操作
 *
 * @author Gmr
 * @since 2024/8/1
 */
@Component
public class AuxiliaryMaterialsHandler implements MaterialHandler{

	@Resource
	private AuxiliaryMaterialsMapper auxiliaryMaterialsMapper;

	@Override
	public void saveMaterial(ProcessMaterials processMaterials) {
		AuxiliaryMaterials auxiliaryMaterials = new AuxiliaryMaterials();
		BeanUtil.copyProperties(processMaterials, auxiliaryMaterials);
		auxiliaryMaterialsMapper.insert(auxiliaryMaterials);
		processMaterials.setMaterialTypeId(auxiliaryMaterials.getId());
	}

	@Override
	public boolean validateMaterial(String code) {
		return auxiliaryMaterialsMapper.selectCount(Wrappers.<AuxiliaryMaterials>lambdaQuery()
				.eq(AuxiliaryMaterials::getCode, code)) > 0;
	}

	@Override
	public Long getMaterialTypeId(String code) {
		return auxiliaryMaterialsMapper.selectOne(Wrappers.<AuxiliaryMaterials>lambdaQuery()
				.eq(AuxiliaryMaterials::getCode, code)).getId();
	}
}
